package com.naodong.shenluntiku.mvp.view.widget.imagepickeview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.naodong.shenluntiku.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hujie on 17/6/15.
 */

public class ImageAddPickerAdapter extends RecyclerView.Adapter {

    private List<String> selectList;
    private boolean isShowDel;
    private boolean isShowAnim;
    private Context context;
    private int deleteLabelRes;
    private int addLabelRes;
    private int maxNum;


    private static final int TYPE_ADD = 1;
    private static final int TYPE_IMAGE = 2;
    private ImageAddPickerListener imageAddPickerListener;

    public void setImageAddPickerListener(ImageAddPickerListener imageAddPickerListener) {
        this.imageAddPickerListener = imageAddPickerListener;
    }

    public ImageAddPickerAdapter(Context context) {
        selectList = new ArrayList<>();
        this.context = context;
    }

    public void add(String imagePath) {
        selectList.add(imagePath);
        notifyDataSetChanged();
    }


    public void replaceAll(List<String> allPath) {
        selectList = allPath;
        notifyDataSetChanged();
    }

    public void addList(List<String> list) {
        selectList.addAll(list);
        notifyDataSetChanged();
    }


    public void remove(int position) {
        selectList.remove(position);
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(context).inflate(R.layout.image_add_pic_item, null);
        return new VViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ImageView deleteIV = (ImageView) holder.itemView.findViewById(R.id.delete);
        ImageView imageIV = (ImageView) holder.itemView.findViewById(R.id.image);

        deleteIV.setImageResource(deleteLabelRes);
        if (getType(position) != TYPE_ADD) {
            deleteIV.setVisibility(View.VISIBLE);
            imageAddPickerListener.onLoadImage(selectList.get(position), imageIV);
        } else {
            deleteIV.setVisibility(View.GONE);
            imageIV.setImageResource(addLabelRes);
        }

        holder.itemView.setOnClickListener(v -> {
            if (getType(position) == TYPE_ADD) {
                if (imageAddPickerListener != null) {
                    imageAddPickerListener.onClickAdd(maxNum - selectList.size());
                }
            }
        });
        deleteIV.setOnClickListener(v -> remove(position));
    }


    public void setShowDel(boolean showDel) {
        isShowDel = showDel;
    }

    public void setShowAnim(boolean showAnim) {
        isShowAnim = showAnim;
    }

    public void setAddLabelRes(int addLabelRes) {
        this.addLabelRes = addLabelRes;
    }

    public void setDeleteLabelRes(int deleteLabelRes) {
        this.deleteLabelRes = deleteLabelRes;
    }

    public void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }

    @Override
    public int getItemCount() {
        return selectList.size() < maxNum ? selectList.size() + 1 : selectList.size();
    }

    public int getType(int position) {
        if (selectList.size() < maxNum) {
            if (position == getItemCount() - 1) {
                return TYPE_ADD;
            }
        }
        return TYPE_IMAGE;

    }


    class VViewHolder extends RecyclerView.ViewHolder {

        public VViewHolder(View itemView) {
            super(itemView);
        }
    }


    public List<String> getSelectList() {
        return selectList;
    }
}
